/**
 * @description 设置本地选项卡及当前所选
 * @param {Object} tabs 选项卡对象
 * @param {String} current 当前选中
 */
function setLoaclTabs(tabs, current) {
  localStorage.setItem("tabs", JSON.stringify(tabs));
  localStorage.setItem("current_tab", current);
}

const initTab = [
  {
    name: "首页",
    router: "index",
    path: "0"
  }
];

const state = {
  //选项卡对象
  tabs: [],
  //当前所选
  current: "index"
};
const actions = {
  /**
   * @description 从本地存储拉取选项卡
   */
  pullTab: ({ commit }) => {
    let tabs = localStorage.getItem("tabs");
    if (tabs) {
      commit("setTabs", JSON.parse(tabs));
    } else {
      tabs = [...initTab];
      commit("setTabs", tabs);
    }
  },
  /**
   * @description 添加选项卡
   * @param {JSON} tab 选项卡对象
   */
  addTab: ({ commit }, tab) => {
    commit("addTab", tab);
  },
  /**
   * @description 删除选项卡
   * @param {Number} index 选项卡索引
   */
  deleteTab: ({ commit }, index) => {
    commit("deleteTab", index);
  },
  /**
   * @description 选择选项卡
   * @param {Number} index 选项卡索引
   */
  selectTab: ({ commit }, index) => {
    commit("setCurrent", index);
  },
  closeAll: ({ commit }) => {
    commit("closeAll");
  },
  closeOther: ({ commit }, index) => {
    commit("closeOther", index);
  }
};
const getters = {
  /**
   * @description 得到选项卡数组
   * @returns {Array} state.tabs 选项卡数组
   */
  getTabs: state => {
    return state.tabs;
  },
  /**
   * @description 得到当前所选
   * @returns {String} 得到当前选择的选项卡
   */
  getCurrent: state => {
    return state.current;
  }
};
const mutations = {
  /**
   * @description 添加选项卡
   */
  addTab: (state, tab) => {
    //查询要添加的选项卡是否已经存在
    for (let i = 0; i < state.tabs.length; i++) {
      if (state.tabs[i].router === tab.router) {
        //如果存在设置当前选项卡为此
        state.current = state.tabs[i].router;
        setLoaclTabs(state.tabs, state.current);
        return;
      }
    }
    //如果选项卡不存在新建
    let index = state.tabs.length;
    state.tabs.push(tab);
    state.current = state.tabs[index].router;
    setLoaclTabs(state.tabs, state.current);
  },
  /**
   * @description 设置选项卡
   */
  setTabs: (state, tabs) => {
    state.tabs = tabs;
    state.current = localStorage.getItem("current_tab") || "index";
    setLoaclTabs(state.tabs, state.current);
  },
  /**
   * @description 删除选项卡
   * @param {Object} state
   * @param {Number} index 选项卡索引
   */
  deleteTab: (state, index) => {
    let current_tab = state.tabs[index];
    state.tabs.splice(index, 1);
    console.log(current_tab);
    if (current_tab.router === state.current) {
      state.current = state.tabs[0].router;
    }
    setLoaclTabs(state.tabs, state.current);
  },
  /**
   * @description 设置当前选项卡
   * @param {Object} state
   * @param {Number} index 索引
   */
  setCurrent: (state, index) => {
    state.current = state.tabs[index].router;
    setLoaclTabs(state.tabs, state.current);
  },
  /**
   * @description 关闭所有
   */
  closeAll: state => {
    state.tabs = [...initTab];
    state.current = state.tabs[0].router;
    console.log(state.tabs);
    setLoaclTabs(state.tabs, state.current);
  },
  /**
   * @description 关闭其他
   */
  closeOther: (state, index) => {
    let current_tab = state.tabs[index];
    let tabs = [...initTab];
    console.log(current_tab, tabs, [...initTab]);
    if (current_tab.router !== tabs[0].router) {
      tabs.push(current_tab);
      state.tabs = tabs;
      state.current = state.tabs[1].router;
    } else {
      state.tabs = tabs;
      state.current = state.tabs[0].router;
    }
    setLoaclTabs(state.tabs, state.current);
  }
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
};
